package com.myhkvideo.yulongbpcdemo.Scheduler;

import com.myhkvideo.yulongbpcdemo.entity.RawOreCllDayInfoData;
import com.myhkvideo.yulongbpcdemo.mapper.RawOreCllDayInfoMapper;
import com.myhkvideo.yulongbpcdemo.service.RawOreCllDayInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

@Component
public class RawOreCllDayInfoScheduler {

    @Autowired
    private RawOreCllDayInfoService rawOreCllDayInfoService;

    @Autowired
    private RawOreCllDayInfoMapper rawOreCllDayInfoMapper;

    String currentDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));


    /**
     * 每5分钟执行一次，将 RawOreCllDayInfoData 数据插入数据库
     */
    @Scheduled(fixedRate = 60 * 60 * 1000) // 5分钟执行一次
    public void insertRawOreCllDayInfoData() {
        try {
            List<RawOreCllDayInfoData> dataList = rawOreCllDayInfoService.getAndConvertRawOreCllDayInfo();
            for (RawOreCllDayInfoData data : dataList) {
                if (rawOreCllDayInfoMapper.checkDataExists(data.getCreateTime()) > 0) {
                    // 数据存在，更新数据
                    rawOreCllDayInfoMapper.updateData(data);
                } else {
                    // 数据不存在，插入新数据
                    data.setId(UUID.randomUUID().toString());
                    rawOreCllDayInfoMapper.insertData(data);
                }

            }
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
        System.out.println(currentDateTime+"全矿日原矿处理量数据获取成功");
    }
}